package controller;

import Utils.RespModel;
import dao.UserDao;
import entity.UserEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.serviceImpl.UserService;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2018/4/8.
 */
@Controller
@RequestMapping()
public class LoginController {

    @Resource
    private UserService userService;

    @RequestMapping("")
    public ModelAndView CasLogin(HttpServletRequest request,HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/login");
        if (cookies==null){
            return mav;
        }
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            if (name.equals("wcb")){
                UserEntity user = new UserEntity();
                String value = cookie.getValue();
                String[] temp = value.split(":");
                if(temp.length == 2){
                    user.setUserName(temp[0]);
                    user.setPassword(temp[1]);
                }
                mav = doLogin(user,response);
                break;
            }
        }
        System.out.println(cookies.toString()+"++++++++++++++++++++++++++++++++++++++++++++++++++");
        return mav;
    }

    @RequestMapping(value = "/doLogin" ,method = RequestMethod.POST)
    public ModelAndView doLogin(UserEntity user, HttpServletResponse response){
        System.out.println("dologin" + user.toString());
        Subject subject = SecurityUtils.getSubject();
        ModelAndView mav = new ModelAndView();
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(),user.getPassword());
            subject.login(token);
            UserEntity userEntity = userService.getByUserName(user.getUserName());
            Cookie ck = new Cookie("wcb",userEntity.getUserName()+":"+userEntity.getPassword());
            ck.setMaxAge(3600);
            ck.setPath("/");
            System.out.println("cooke添加成功");
            response.addCookie(ck);
            mav.setViewName("/homePage");
            mav.addObject("menu", "index");
            mav.addObject("user",userEntity);
            return mav;
        }catch (Exception e){
            e.printStackTrace();
            mav.setViewName("/login");
            mav.addObject("errorMsg","用户名或密码错误！");
            return mav;
        }
    }

    @RequestMapping(value = "/registerUser")
    @ResponseBody
    public RespModel register(UserEntity user){
        UserEntity userEntity = userService.getByUserName(user.getUserName());
        RespModel respModel = new RespModel();
        if (userEntity == null && user != null){
            userService.registerUser(user);
            respModel.setCode(RespModel.RespCode.SUCCESS.getCode());
            respModel.setDesc("注册成功！");
            return respModel;
        }else{
            respModel.setCode(RespModel.RespCode.SYS_EXCEPTION.getCode());
            respModel.setDesc("用户已存在！");
            return respModel;
        }
    }

    @RequestMapping(value = "/logout")
    @ResponseBody
    public RespModel logout(HttpServletRequest request,HttpServletResponse response){
        RespModel respModel = new RespModel();
        Cookie[] cookies = request.getCookies();
        if (null==cookies) {
            System.out.println("没有cookie==============");
        } else {
            for(Cookie cookie : cookies){
                if(cookie.getName().equals("wcb")){
                    cookie.setValue(null);
                    cookie.setMaxAge(0);// 立即销毁cookie
                    cookie.setPath("/");
                    System.out.println("被删除的cookie名字为:"+cookie.getName());
                    response.addCookie(cookie);
                    break;
                }
            }
        }
        respModel.setCode(RespModel.RespCode.SUCCESS.getCode());
        return respModel;
    }
}
